<script setup>
import {backProcessDocList} from "@/utils/backList.js";
import {reject} from "@/api/document/processDocument.js";
import AdviceAdd from "@/views/document/common/advice/adviceAdd.vue";
import {getDocumentProcessResultEnum} from "@/utils/menu.js";
import AdviceList from "@/views/document/common/advice/adviceList.vue";

const prop = defineProps({
  documentInfo: {
    type: Object
  },
  isBack: {
    type: Boolean
  },
  isApproved: {
    type: Boolean
  },
  approvedFunction: {
    type: Function
  },
  isRefreshAdvice: {
    type: Boolean
  }
})
const docId = prop.documentInfo.id
const adviceDialogOpen = ref(false)
const adviceDialogTitle = ref('')
const processResult = ref('')
const processResultEnum = getDocumentProcessResultEnum()

function handleOpenAdvice(result) {
  adviceDialogOpen.value = true
  adviceDialogTitle.value =`${prop.documentInfo.status}意见 (${result})`
  processResult.value = result
}

const handleCloseAdvice = () => {
  adviceDialogOpen.value = false
}

async function handleSubmitAdvice(form) {
  form.id = docId
  if (processResult.value === processResultEnum.approved) {
    await prop.approvedFunction(form)
  } else {
    await reject(form)
  }
  adviceDialogOpen.value = false
  backProcessDocList()
}

</script>

<template>
  <el-row>
    <el-col :span="12" :xs="24" class="left">
      <el-row>
        <el-form-item>
          <el-button @click="backProcessDocList">返 回</el-button>
          <slot />
          <el-button type="primary" @click="handleOpenAdvice(processResultEnum.reject)" v-if="isBack" >驳 回</el-button>
          <el-button type="success" @click="handleOpenAdvice(processResultEnum.approved)" v-if="isApproved">通 过</el-button>
        </el-form-item>
      </el-row>
    </el-col>
  </el-row>
  <AdviceAdd :title="adviceDialogTitle" :isOpen="adviceDialogOpen"
             @handleSubmitAdvice="handleSubmitAdvice"
             @close="handleCloseAdvice"/>
  <!-- 意见列表 -->
  <AdviceList :isRefreshAdvice="isRefreshAdvice"/>
</template>

